package com.chuanjiabao.wisdom.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.chuanjiabao.wisdom.MainActivity;
import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.application.FApplication;
import com.chuanjiabao.wisdom.bean.RefreshMessageEvent;
import com.chuanjiabao.wisdom.databinding.ActivityUserBinding;
import com.chuanjiabao.wisdom.greendao.gen.UpdateFileTeamDao;
import com.chuanjiabao.wisdom.listener.OnBHClickListener;
import com.chuanjiabao.wisdom.ui.ActivityStack;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.util.GlideCacheUtil;
import com.chuanjiabao.wisdom.util.HTTPAPI;
import com.chuanjiabao.wisdom.util.LogUtil;
import com.chuanjiabao.wisdom.util.SpUtils;
import com.chuanjiabao.wisdom.util.dialog.ArriveDialog;
import com.chuanjiabao.wisdom.util.dialog.HintDeleteDialog;
import com.chuanjiabao.wisdom.util.dialog.InCommonDialog;
import com.chuanjiabao.wisdom.util.dialog.WxHintDialog;
import com.lzy.okgo.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import okhttp3.Call;
import okhttp3.Response;

public class UserActivity extends BaseActivity {

    private ActivityUserBinding binding;
    private UpdateFileTeamDao teamDao = FApplication.getInstance().getDaoSession().getUpdateFileTeamDao();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_user);
        binding.setActivity(this);
        initView();
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        String cacheSize = GlideCacheUtil.getInstance().getAllCacheSize(this);
        binding.tvModuleCache.setText(cacheSize);

        binding.rvModuleLeft.setOnClickListener(this);
        binding.tvModuleRight.setOnClickListener(this);
        binding.llModuleInfo.setOnClickListener(this);
        binding.llModuleCache.setOnClickListener(this);
        binding.llModuleDeal.setOnClickListener(this);
        binding.llModuleSecret.setOnClickListener(this);
        binding.tvModuleOut.setOnClickListener(this);
        binding.tvModuleDestory.setOnClickListener(this);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvModuleRight:
                ArriveDialog arriveDialog = new ArriveDialog(this);
                arriveDialog.show();
                arriveDialog.setOnBHClickListener(position -> {
                    RefreshMessageEvent event = new RefreshMessageEvent();
                    event.setType("main");
                    event.setId(position + "");
                    EventBus.getDefault().post(event);
                    ActivityStack.getScreenManager().popAllActivityExceptOne(MainActivity.class);
                });
                break;
            case R.id.llModuleCache:
                new HintDeleteDialog(this)
                        .setTitle("清除缓存")
                        .setContent("确定要清除缓存？")
                        .setYes("确认清除")
                        .setOnBHClickListener(new OnBHClickListener() {
                            @Override
                            public void onConfirmClick() {
                                GlideCacheUtil.getInstance()
                                        .clearImageAllCache(UserActivity.this);
                                binding.tvModuleCache.setText("0B");
                            }
                        })
                        .show();
                break;
            case R.id.tvModuleOut:
                InCommonDialog inCommonDialog = new InCommonDialog(this);
                inCommonDialog.setTitleText("确定退出登陆?", "退出");
                inCommonDialog.show();
                inCommonDialog.setOnBHClickListener(new OnBHClickListener() {
                    @Override
                    public void onConfirmClick() {
                        showProgressDialog();
                        HTTPAPI.getInstance().getLoginOut(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                cancelProgressDialog();
                                teamDao.deleteAll();

                                Intent intentDelete = new Intent();
                                intentDelete.setAction("MODULE_ABOUT_RELATION");
                                intentDelete.putExtra("module_funcation", "1");
                                sendBroadcast(intentDelete);

                                SpUtils.clear(UserActivity.this);
                                startActivity(new Intent(UserActivity.this, LoginActivity.class));
                                ActivityStack.getScreenManager().popAllActivityExcept(LoginActivity.class);
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                cancelProgressDialog();
                                LogUtil.e("退出失败" + e.getMessage());
                            }
                        });
                    }
                });
                break;
            case R.id.rvModuleLeft:
                finish();
                break;
            case R.id.llModuleInfo:
                startActivity(new Intent(this, UserInfoActivity.class));
                break;
            case R.id.llModuleDeal:
                WxHintDialog dialog = new WxHintDialog(this);
                dialog.setContent("http://home.zzwisdom.com/deal.html");
                dialog.setTitleHint("用户协议");
                dialog.setBtnHint("确定");
                dialog.setOneBtn();
                dialog.show();
                break;
            case R.id.llModuleSecret:
                WxHintDialog dialog2 = new WxHintDialog(this);
                dialog2.setContent("https://home.zzwisdom.com/private.html");
                dialog2.setTitleHint("隐私政策");
                dialog2.setBtnHint("确定");
                dialog2.setOneBtn();
                dialog2.show();
                break;
            case R.id.tvModuleDestory:
                WxHintDialog dialog3 = new WxHintDialog(this);
                dialog3.setContent("http://home.zzwisdom.com/demo/index/cancelAccount");
                dialog3.setTitleHint("注销账户须知");
                dialog3.setBtn2Hint("注销");
                dialog3.setOnBHClickListener(new OnBHClickListener() {
                    @Override
                    public void onConfirmClick() {
                        cancelAccount();
                    }
                });
                dialog3.show();
                break;
        }
    }

    private void cancelAccount() {
        showProgressDialog();
        HTTPAPI.getInstance().getCancelOut(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                cancelProgressDialog();
                teamDao.deleteAll();

                Intent intentDelete = new Intent();
                intentDelete.setAction("MODULE_ABOUT_RELATION");
                intentDelete.putExtra("module_funcation", "1");
                sendBroadcast(intentDelete);

                SpUtils.clear(UserActivity.this);
                startActivity(new Intent(UserActivity.this, LoginActivity.class));
                ActivityStack.getScreenManager().popAllActivityExcept(LoginActivity.class);
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                cancelProgressDialog();
                LogUtil.e("注销失败" + e.getMessage());
            }
        });
    }

}

